#include "dialogmain.h"
#include "ui_dialogmain.h"
#include <QDebug>
#include <QProcess>
#include <QTimer>

DialogMain::DialogMain(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogMain)
{
    ui->setupUi(this);
}

DialogMain::~DialogMain()
{
    delete ui;
}

void DialogMain::onParseArgs(const QString& args)
{
    qDebug() << args;
    mArgsList = args.split(" ", QString::SkipEmptyParts);
    if (mArgsList.length()) {
        if (0 == mArgsList.at(0).compare("reboot")) {
            int delaySec = mArgsList.at(1).toInt() * 1000;
            QTimer::singleShot(delaySec, this, SLOT(onDelayProc()));
        } else if (0 == mArgsList.at(0).compare("forever")) {
            int delaySec = mArgsList.at(1).toInt() * 1000;
            QTimer::singleShot(delaySec, this, SLOT(onDelayProc()));
        }
    }
}

void DialogMain::onDelayProc()
{
    if (mArgsList.length()) {
        if (0 == mArgsList.at(0).compare("reboot")) {
            QProcess::startDetached(mArgsList.at(2));

            QTimer::singleShot(100, this, SLOT(onMenuExit()));
        } else if (0 == mArgsList.at(0).compare("forever")) {
            QProcess::startDetached(mArgsList.at(2));

            int delaySec = mArgsList.at(1).toInt() * 1000;
            QTimer::singleShot(delaySec, this, SLOT(onDelayProc()));
        }
    }
}

void DialogMain::onMenuExit()
{
    //qApp->quit();
}
